<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin: 20px 0">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/home/goods' }">商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4" style="margin-left: 20px">
          <!-- 点击添加用户按钮 -->
          <el-button type="primary" @click="addGoodsVisible = true">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 添加商品对话框 - 弹窗 -->
      <el-dialog title="收货地址" :visible.sync="addGoodsVisible">
        <el-form :model="goodsForm">
          <el-form-item label="商品名称" :label-width="formLabelWidth">
            <el-input v-model="goodsForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="活动名称" :label-width="formLabelWidth">
            <el-input v-model="goodsForm.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 商品列表表格 -->
      <template>
        <el-table :data="goodsList" stripe style="width: 100%" border :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }">
          <el-table-column type="index" label="#" width="100"> </el-table-column>
          <el-table-column prop="goods_name" label="商品名称" width="490"> </el-table-column>
          <el-table-column prop="goods_price" label="商品价格" width="130"> </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" width="130"> </el-table-column>
          <el-table-column prop="add_time" label="创建时间"> </el-table-column>
          <el-table-column label="状态" width="70">
            <!-- 插槽 -->
            <!-- 可以拿到这一行的数据 -->
            <template slot-scope="scope">
              <!-- <p>{{ scope.row }}</p> -->
              <el-switch v-model="scope.row.goods_state"> </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <!-- 编辑用户 -->
              <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoods(scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  name: 'GoodsList',
  data() {
    return {
      goodsList: [],
      total: 0,
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      // 添加商品列表
      // 控制弹窗开关
      addGoodsVisible: false,
      goodsForm: {},
      formLabelWidth: '100px',
    }
  },
  methods: {
    async getGoodsList() {
      const { data } = await this.$http.get('goods', {
        params: this.queryInfo,
      })
      // 数据二次处理
      data.data.goods.forEach((item) => {
        let date = new Date(item.add_time)
        item.add_time = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
      })
      this.goodsList = data.data.goods
      console.log(this.goodsList)
      this.total = data.data.total
    },
    handleSizeChange(size) {
      // 改变条数
      this.queryInfo.pagesize = size
      // 再次请求数据
      this.getGoodsList()
    },
    handleCurrentChange(num) {
      // 改变页数
      this.queryInfo.pagenum = num
      // 再次请求数据
      this.getGoodsList()
    },
    // 添加商品弹窗
    addGoodsList() {
      this.addGoodsVisible = true
    },
    async removeGoods(role) {
      console.log(role)
      const { data } = await this.$http.delete(`goods/${role.goods_id}`)
      if (data.meta.status !== 200) return this.$message.error('删除失败, 请重试!')
      this.$message.success('删除成功')
      this.getGoodsList()
    },
  },
  created() {
    this.getGoodsList()
  },
}
</script>
<style lang="less">
.el-table {
  margin: 20px 0;
}
</style>
